var service_id_value = -1;//默认服务编号
var api_type_value = 2;//默认tab标签状态
var pagesize = 20; 

//tab切换
function setTab(name,cursel){
  $('._tab_list').removeClass('hr');
  $(cursel).addClass('hr');

  //获取当前类型值
  api_type_value = name.replace('con_one_','');
  //$('._api_type_value').val(api_type_value);

  //alert("service_id:"+service_id_value+"----"+",type_value:"+api_type_value);
  if(service_id_value==-1){
    return;
  }

  //获取数据
  getApiList({
    page:1,
    pagesize:pagesize,
    //service_id : $('._api_service_id_value').val(),
    //api_type : $('._api_type_value').val()
    service_id:service_id_value,
    api_type:api_type_value
  });

}

//切换分页时
function gotopage(current_page){
  // //获取选择服务项
  // var service_id = $('._api_service_id_value').val();
  // //获取tab项
  // var api_type = $('._tab_list.hr').attr('data-id');

  if(service_id_value==-1){
    return;
  }
  
  //获取分页数据
  getApiList({page:current_page,api_type:api_type_value,service_id:service_id_value,pagesize:pagesize});

}

//获取分页数据
function getApiList(option){
  var default_option = {
    page : 1,//当前页码
    pagesize : pagesize,
    // service_id : $('._api_service_id_value').val(),
    // api_type : $('._api_type_value').val()
    service_id:-1,
    api_type:2
  }

  option = $.extend({},default_option,option);
  $.ajax({
    url:'/apiworks/ajaxApiList.html',
    type:'POST',
    dataType:'json',
    async:true,
    data:option,
    success:function (req){
      if(req.status==200){
        //console.log(list);
        //渲染数据
        var html = template('api_list_published', req.data);
        
        document.getElementById('api_list_published_content').innerHTML = html;

        //获取分页html代码
        var page = _37degree_setpage(req.data.page,req.data.totalpage);//当前页码，总页数
        $('._page_info').html(page);

      }else{
        show_msg({msg:req.msg,type:'error',time:2000});
      }
      return false;
    },
    error:function (){
      show_msg({msg:'系统异常',type:'error',time:2000});
    }
  });
}

$(function (){
  // 初始化
  if( $('._apilist_service_id').val()){
    service_id_value = $('._apilist_service_id').val();
    api_type_value = $('._apilist_api_type').val();

    $('._tab_list').removeClass('hr');
    $('._tab_list').each( function (){
      var that = $(this);
      if(that.attr('data-id')==api_type_value){
        that.addClass('hr');
      }
      
    });

    getApiList({
      page:1,
      pagesize:pagesize,
      service_id:service_id_value,
      api_type : api_type_value
    });

  }
  
    template.helper('dateFormat', dateFormat);//注册日期格式化方法

    //服务切换
    $("#service-select div ul li").click(function(){
  	 var that = $(this);
     //$('._api_service_id_value').val(that.attr('data-value')); 
     service_id_value = that.attr('data-value');
     // alert("service_id:"+service_id_value+"----"+",type_value:"+api_type_value);

     //ajax获取数据
     getApiList({
      page:1,
      pagesize:pagesize,
      service_id:service_id_value,
      api_type : api_type_value
     });

  });

  //点击取消，隐藏确认框 
  $("._close_btn").on('click', function () {
      $("#delBox,#xiajiaBox,#backBox,#publishBox,#upBox").fadeOut("fast");
   });

  //删除
  $(document)
    .on('click','._api_list_del',function (){
        //显示确认框
      $("#delBox").fadeIn("slow");
      var that = $(this);
      
      $("._confirm_btn").unbind('click').click(function(){
    	  $("#delBox").fadeOut("fast");
          $.ajax({
              url:'/apiworks/apiDel.html',
              type:'get',
              dataType:'json',
              async:true,
              data:{'service_id':that.attr('data-serviceid'),'api_id':that.attr('data-id')},
              success:function (req){
                if(req.status==200){
                  that.parent().parent('._raw_api').remove();
                  show_msg({msg:req.msg,type:'ok',time:2000});
                  location.reload(true)
                }else{
                  show_msg({msg:req.msg,type:'error',time:2000});
                }
                return false;
              },
              error:function (){
                show_msg({msg:'系统异常',type:'error',time:2000});
              }
            });
      });

	 })
  .on('click','._api_publish',function (){
	   //发布
      var that = $(this);
      $("#publishBox").fadeIn("slow");
      $('._confirm_publish_btn').unbind('click').click(function (){
        $('#publishBox').fadeOut('fast');

        $.ajax({
          url:'/apiworks/apiPublish.html',
          type:'get',
          dataType:'json',
          async:true,
          data:{'service_id':that.attr('data-serviceid'),'api_id':that.attr('data-apiid'),'src_api_status':that.attr('data-srcstatus')},
          success:function (req){
            if(req.status==200){
              that.parent().parent('._raw_api').remove();
              show_msg({msg:req.msg,type:'ok',time:2000});
              setTab('con_one_3',$('._tab_list.hr'))
            }else{
              show_msg({msg:req.msg,type:'error',time:2000});
            }
            return false;
          },
          error:function (){
            show_msg({msg:'系统异常',type:'error',time:2000});
          }
        });

      });
        
	 })
  .on('click','._api_setdown',function (){
      //下架
      var that = $(this);
      if(that.attr('data-api-status')==0){
        $('._confirm_down_btn').unbind('click');
        show_msg({msg:'更新审核中或失败，暂不能下架',type:'warning'});
        return;
      }
      $("#xiajiaBox").fadeIn("slow");
      $('._confirm_down_btn').unbind('click').click(function (){
        $("#xiajiaBox").fadeOut("fast");
        $.ajax({
          url:'/apiworks/apiSetDown.html',
          type:'post',
          dataType:'json',
          async:true,
          data:{
            'service_id':that.attr('data-serviceid'),'api_id':that.attr('data-apiid'),
            api_name:that.attr('data-api-name'),service_name:that.attr('data-service-name'),
            api_name_en:that.attr('data-api-name-en'),service_name_en:that.attr('data-service-name-en')
          },
          success:function (req){
            if(req.status==200){
              //getApiList();
              that.parent().parent('._raw_api').remove();
              show_msg({msg:req.msg,type:'ok',time:2000});
              setTab('con_one_2',$('._tab_list.hr')[0])
            }else{
              show_msg({msg:req.msg,type:'error',time:2000});
            }
            return false;
          },
          error:function (){
            show_msg({msg:'系统异常',type:'error',time:2000});
          }
        });
      });

    })
  .on('click','._api_callback',function (){
    //撤回
    var that = $(this);
    var type = that.attr('data-type');
    if(type=='two'){
      $('._callback_p').hide();
      $('._callback_two').show();
    }else if(type=='three'){
      $('._callback_p').hide();
      $('._callback_three').show();
    }else if(type=='four'){
      $('._callback_p').hide();
      $('._callback_four').show();
    }else{
      $('._callback_p').hide();
      $('._callback_one').show();
    }
    $("#backBox").fadeIn("slow");
    $("._confirm_back_btn").unbind('click').click(function (){
      $("#backBox").fadeOut("fast");
      $.ajax({
        url:'/apiworks/apiStatusCallback.html',
        type:'post',
        dataType:'json',
        async:true,
        data:{
          'service_id':that.attr('data-serviceid'),'api_id':that.attr('data-apiid'),
          'api_status':that.attr('data-api-status'),'api_auth_status':that.attr('data-api-auth-status'),
          'src_api_status':that.attr('data-src-api-status')
        },
        success:function (req){
          if(req.status==200){
            //getApiList();
            that.parent().parent('._raw_api').remove();
            show_msg({msg:req.msg,type:'ok',time:800});
            setTab('con_one_1',$('._tab_list.hr')[0])
          }else{
            show_msg({msg:req.msg,type:'error',time:800});
          }
          return false;
        },
        error:function (){
          show_msg({msg:'系统异常',type:'error',time:1000});
        }
      });

    });

      

  })
  .on('click','._api_resetup',function (){
    //重新上架
    var that = $(this);
      $('#upBox').fadeIn('slow');

      $('._confirm_up_btn').unbind('click').click(function (){
          $('#upBox').fadeOut('fast');

          $.ajax({
            url:'/apiworks/apiResetUp.html',
            type:'post',
            dataType:'json',
            async:true,
            data:{'service_id':that.attr('data-serviceid'),'api_id':that.attr('data-apiid')},
            success:function (req){
              if(req.status==200){
                //getApiList();
                that.parent().parent('._raw_api').remove();
                show_msg({msg:req.msg,type:'ok',time:2000});
                setTab('con_one_4',$('._tab_list.hr'))
              }else{
                show_msg({msg:req.msg,type:'error',time:2000});
              }
              return false;
            },
            error:function (){
              show_msg({msg:'系统异常',type:'error',time:2000});
            }
          });

      });
      

  })
  .on('click','._api_update',function (){
    var that = $(this);
    var status = that.attr('data-apistatus');
    var auth_status = that.attr('data-api-auth-status');
    if(status==0){
      show_msg({msg:'更新审核中或失败，暂不能更新'});return;
    }
    var api_id = that.attr('data-apiid');
    window.open('/apiworks/apiUpdate.html?api_id='+api_id+'&auth='+auth_status,'_blank');
  })
  .on('mouseover mouseout','._fail_auth_span',function (event){
    //浮动效果
    var that = $(this);
    if(event.type == "mouseover"){
      that.children('span._child_fail_span').css('display','block');
    }else if(event.type == "mouseout"){
      that.children('span._child_fail_span').hide();
    }
  })

  ; 
  
});